<?php

namespace Admin \ Controller;

use Think \ Controller;

class IndexController extends CommonController {

    public $avoidAuth = array(
        'login',
        'logout',
        'checkLogin',
    );

    public function _initialize() {
        $this->setAvoidAuth($this->avoidAuth);
        parent :: _initialize();
    }

    public function index() {
        $this->redirect('Score/storelist');
        //$this->redirect('Activity/index');
    }

    public function login() {
        $this->display('login');
    }

    public function checkLogin() {
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        if ($username != 'admin') {
            $gc = M('score_storeuser');
            $map['name'] = $username;
            $map['state'] = 1;
            $userTure = $gc->where($map)->find();
          
            $euser = M('exchange_storeuser')->where($map)->find();

            if (!$userTure && !$euser) {
                $data['info'] = "无效账号";
            } elseif ($userTure['pwd'] != $password && $euser['pwd'] != $password) {
                $data['info'] = "密码错误";
            } elseif ($userTure['state'] != 1 && $euser['state'] != 1) {
                $data['info'] = "账号异常";
            } else {
               
                $_SESSION['USER'] = $userTure['name']?$userTure['name']:$euser['name'];
                $_SESSION['sid'] = $userTure['sid'] ? $userTure['sid'] : 0;
                $_SESSION['esid'] = $euser['sid'] ? $euser['sid'] : 0;
                $arr['pcip'] = getcip();
                $arr['pclogintime'] = date('Y-m-d H:i:s');
                if ($userTure) {
                    $gc->where('id=' . $userTure['id'])->save($arr); //更新登陆信息
                }
                if ($euser) {
                    M('exchange_storeuser')->where('id=' . $euser['id'])->save($arr); //更新登陆信息
                }
             
                $data['info'] = "登录成功";
                $data['data'] =  !$userTure?$euser['sid']:$userTure['sid'];
                $data['status'] = 2;
                $data['url'] = empty($userTure['sid'])?'exchange':'score';
            }
        } else {
            if ('admin' == $username && 'admin123' == $password) {
                $_SESSION['USER'] = $username;
                $data['status'] = 1;
                $str = '登录ip地址:'.getcip().',登录时间:'.date('Y-m-d H:i:s').PHP_EOL;
                file_put_contents("./app/Admin/Controller/adminlog.txt", $str, FILE_APPEND);//写入登录日志
            } else {
                $data['info'] = "用户名/密码有误！";
            }
        }
        $this->ajaxReturn($data);
    }

    //登出
    public function logout() {
        unset($_SESSION['USER']);
        redirect(__APP__ . '/Index/login', 0, '退出成功，页面跳转中...');
    }

    //倒入验证码
    public function verify() {
        $verify = new \ Think \ Verify();
        $verify->entry(1);
    }

}
